package com.mentaleye.controller;

import java.util.*;

import com.mentaleye.bean.*;
import com.mentaleye.biz.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.dal.model.TimeTableExample;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.DateUtils;

@RequestMapping(value = "/timetable")
@Controller
public class TimeTableController {

	@Autowired private CurriculumBiz curriculumBiz;
	@Autowired private CurriculumTempRlateBiz curriculumTempRlateBiz;
	@Autowired private TimeTableBiz timeTableBiz;
	@Autowired private TimeTableClassBiz timeTableClassBiz;
	@Autowired private TaskDateBiz taskDateBiz;
	@Autowired private OaClassBiz oaClassBiz;
	@Autowired private TeacherBiz teacherBiz;

	@RequestMapping(value = "/index")
	public ModelAndView index(@RequestParam("oaClassId") Integer oaClassId,
			@RequestParam("tempId") Integer curriculumTempNo,@RequestParam("timeTableId") Integer timeTableId,
							  @RequestParam(value = "isClass",required = false) boolean isClass){
		ModelAndView view = new ModelAndView("tc/timetable/timeTableIndex");
		List<TimeTableBean> timeTable = timeTableBiz.queryByTimeTableId(timeTableId);
		double days = 0;
		if(timeTable == null || timeTable.size()<=0){
			List<CurriculumTempRlateBean> currList = curriculumTempRlateBiz.queryByJoin(curriculumTempNo);
			for (CurriculumTempRlateBean curriculumTempRlateBean : currList) {
				days = days + curriculumTempRlateBean.getCurriculum().getCurriculumDay();
			}
			int length = (int) (days + 1);
			List<Date> datelist = taskDateBiz.getWorkDateByStringDateAndLength(DateUtils.getStrByDate(
					oaClassBiz.queryOaclassData(oaClassId, null).getOaclassOpen(), "yyyy-MM-dd"), length, false);
			List<WorkTimeBean> tasklist = taskDateBiz.getWorkTimeByMouth("0", 0);
			WorkTimeBean workTime = null;
			for (WorkTimeBean workTimeBean : tasklist) {
				if(workTimeBean.getWorkDefault()){
					workTime = workTimeBean;
				}
			}
			List<TimeTableBean> timeTableList = setTimeTableData(currList, timeTableId, workTime, datelist);
			timeTableBiz.addList(timeTableList);
		}
		view.addObject("curriculumTempNo", curriculumTempNo);
		view.addObject("timeTableId", timeTableId);
		view.addObject("teacherList", JSONObject.toJSONString(teacherBiz.queryAllTeacher()));
		view.addObject("teacherSize", teacherBiz.queryAllTeacher());
		view.addObject("isClass",isClass);
		return view;
	}

	@RequestMapping(value = "/showTable")
	@ResponseBody
	public String showTable(@RequestParam("oaClassId") Integer oaClassId){
		TimeTableClassBean bean = timeTableClassBiz.queryTimeTableClassByOaClassId(oaClassId);
		return JSONObject.toJSONString(bean);
	}
	
	@RequestMapping(value = "/getData/{id}")
	public @ResponseBody String getData(@PathVariable("id") Integer timeTableId){
		List<TimeTableBean> timeTable = timeTableBiz.queryByTimeTableId(timeTableId);
		return JSONObject.toJSONString(timeTable);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(@RequestParam("curriculumTempNo") Integer curriculumTempNo,
			@RequestParam("timeTableId") Integer timeTableId,@RequestParam("date") String date){
		ModelAndView view = new ModelAndView("tc/timetable/timeTableAdd");
		view.addObject("tempRlateList", curriculumBiz.queryCurriculum(curriculumTempNo));
		view.addObject("timeTableId", timeTableId);
		view.addObject("date", date);
		return view;
	}
	
	@RequestMapping(value = "/update")
	public ModelAndView update(@RequestParam("id") Integer id){
		ModelAndView view = new ModelAndView("tc/timetable/timeTableModify");
		TimeTableBean timeTable = timeTableBiz.queryById(id);
		view.addObject("teacherList", teacherBiz.queryAllTeacher());
		view.addObject("timeTable", timeTable);
		view.addObject("classDate", DateUtils.getStrByDate(timeTable.getBeginTime(), "yyyy-MM-dd"));
		view.addObject("begintime", DateUtils.getStrByDate(timeTable.getBeginTime(), "HH:mm:ss"));
		view.addObject("endtime", DateUtils.getStrByDate(timeTable.getEndTime(), "HH:mm:ss"));
		return view;
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(@RequestParam("id")Integer id){
		return JSONObject.toJSONString(timeTableBiz.removeById(id));
	}
	
	@RequestMapping(value = "/save")
	public @ResponseBody String save(TimeTableBean timeTableBean,@RequestParam(value = "date",required = false)Date date,
			@RequestParam("begintime")String begintime,@RequestParam("endtime")String endtime,
			@RequestParam("checkBox")String checkBox,@RequestParam(value = "startDate",required = false)Date startDate){
		boolean success = false;
		if(1 == Integer.valueOf(checkBox)){
			List<WorkTimeBean> tasklist = taskDateBiz.getWorkTimeByMouth("0", 0);
			WorkTimeBean workTime = null;
			for (WorkTimeBean workTimeBean : tasklist) {
				if(workTimeBean.getWorkDefault()){
					workTime = workTimeBean;
				}
			}
			List<TimeTableBean> timeTableList = timeTableBiz.queryByBeginTime(timeTableBean.getTimeTableId(), 
					DateUtils.getStrByDate(date, "yyyy-MM-dd"));
			if(timeTableList != null){
				List<Date> dateList = taskDateBiz.getWorkDateByStringDateAndLength(
						DateUtils.getStrByDate(startDate, "yyyy-MM-dd"), timeTableList.size(), false);
				for (int i = 0; i < timeTableList.size(); i++) {
					timeTableList.get(i).setBeginTime(DateUtils.getDateByStr(DateUtils.getStrByDate(
							dateList.get(i), "yyyy-MM-dd")+" "+workTime.getOfficeHour()+":00", "yyyy-MM-dd HH:mm:ss"));
					timeTableList.get(i).setEndTime(DateUtils.getDateByStr(DateUtils.getStrByDate(
							dateList.get(i), "yyyy-MM-dd")+" "+workTime.getClosingTime()+":00", "yyyy-MM-dd HH:mm:ss"));
				}
				success = timeTableBiz.modifyByList(timeTableList).getIsSuccess();
			}
		}else{
			success = true;
		}
		CurriculumBean curr = curriculumBiz.getCurriculumByNo(timeTableBean.getCurriculumId());
		Date beginTime = DateUtils.getDateByStr(
				DateUtils.getStrByDate(date, "yyyy-MM-dd") + " " + begintime, "yyyy-MM-dd HH:mm:ss");
		Date endTime = DateUtils.getDateByStr(
				DateUtils.getStrByDate(date, "yyyy-MM-dd") + " " + endtime, "yyyy-MM-dd HH:mm:ss");
		timeTableBean.setBeginTime(beginTime);
		timeTableBean.setEndTime(endTime);
		timeTableBean.setCurriculumName(curr.getCurriculumName());
		if(success){
			success = timeTableBiz.addByBean(timeTableBean).getIsSuccess();
		}
		return JSONObject.toJSONString(new CommonResult(success));
	}
	
	@RequestMapping(value = "/updatesave")
	public @ResponseBody String updatesave(TimeTableBean timeTableBean,@RequestParam(value = "classDate")Date classDate,
					@RequestParam(value = "begintime")String begintime,@RequestParam(value = "endtime")String endtime,
										   @RequestParam(value = "lecture")String lecture){
		Date beginTime = DateUtils.getDateByStr(DateUtils.getStrByDate(classDate, "yyyy-MM-dd")+" "+begintime, "yyyy-MM-dd HH:mm:ss");
		Date endTime = DateUtils.getDateByStr(DateUtils.getStrByDate(classDate, "yyyy-MM-dd")+" "+endtime, "yyyy-MM-dd HH:mm:ss");
		timeTableBean.setBeginTime(beginTime);
		timeTableBean.setEndTime(endTime);
		CommonResult result = timeTableBiz.modify(timeTableBean);
		if (!"date".equals(lecture) && result.getIsSuccess()){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("teacherId",timeTableBean.getTeacherId());
			map.put("timeTableId",timeTableBean.getTimeTableId());
			if ("curriculum".equals(lecture)){
				map.put("curriculumId",timeTableBean.getCurriculumId());
			}else {
				CurriculumBean curriculum = curriculumBiz.getCurriculumByNo(timeTableBean.getCurriculumId());
				map.put("curriculumStep",curriculum.getCurriculumStep());
			}
			timeTableBiz.modifyTeacherByMap(map);
		}
		return JSONObject.toJSONString(result);
	}
	
	private List<TimeTableBean> setTimeTableList(List<CurriculumTempRlateBean> currlist,Integer timeTableId){
		List<TimeTableBean> timeTableList = new ArrayList<TimeTableBean>();
		for (int j = 0;j<currlist.size();j++) {
			for (int i = 0; i < currlist.get(j).getCurriculum().getCurriculumDay(); i++) {
				TimeTableBean timeTableBean = new TimeTableBean();
				timeTableBean.setTimeTableId(timeTableId);
				timeTableBean.setCurriculumId(currlist.get(j).getCurriculumNo());
				timeTableBean.setCurriculumName(currlist.get(j).getCurriculum().getCurriculumName());
				timeTableList.add(timeTableBean);
			}
		}
		return timeTableList;
	}
	
	private List<TimeTableBean> setTimeTableData(List<CurriculumTempRlateBean> list,Integer timeTableId,WorkTimeBean workTime,List<Date> datelist){
		List<TimeTableBean> timeTableList = setTimeTableList(list,timeTableId);
		for (int j=0;j<timeTableList.size();j++) {
			timeTableList.get(j).setBeginTime(DateUtils.getDateByStr(
					DateUtils.getStrByDate(datelist.get(j), "yyyy-MM-dd")+" "+workTime.getOfficeHour(), "yyyy-MM-dd HH:mm"));
			timeTableList.get(j).setEndTime(DateUtils.getDateByStr(
					DateUtils.getStrByDate(datelist.get(j), "yyyy-MM-dd")+" "+workTime.getClosingTime(), "yyyy-MM-dd HH:mm"));
		}
		return timeTableList;
	}
}
